package com.example.livestockbreedingapplication.slice;

import com.example.livestockbreedingapplication.ResourceTable;
import com.example.livestockbreedingapplication.data.Result;
import com.example.livestockbreedingapplication.domain.CollectData;
import com.example.livestockbreedingapplication.domain.Data;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.zzrv5.mylibrary.ZZRCallBack;
import com.zzrv5.mylibrary.ZZRHttp;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.agp.window.dialog.ToastDialog;

public class SystemMainAbilitySlice extends AbilitySlice implements Component.ClickedListener {
    Text userName;
    Image setting;
    Image historyData;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_system_main);
        userName = (Text) findComponentById(ResourceTable.Id_username);
        setting = (Image)findComponentById(ResourceTable.Id_setting);
        historyData = (Image)findComponentById(ResourceTable.Id_history_data);
        historyData.setClickedListener(this);
        setting.setClickedListener(this);
        DirectionalLayout toLogin = (DirectionalLayout)findComponentById(ResourceTable.Id_to_login);
        toLogin.setClickedListener(this);

        String url =  "http://123.56.152.169:8088/sensorData/";
        Gson gson = new GsonBuilder().serializeNulls().setDateFormat("yyyy-MM-dd").create();
        ZZRHttp.get(url, new ZZRCallBack.CallBackString() {
            @Override
            public void onFailure(int i, String s) {
                new ToastDialog(SystemMainAbilitySlice.this)
                        .setText("网络不稳定")
                        .show();
            }

            @Override
            public void onResponse(String s) {
                Result result = gson.fromJson(s,Result.class);
                if (result.getCode() == 1){
                    Data data = gson.fromJson(result.getData().get(0),Data.class);
                }
            }
        });

    }


    @Override
    public void onActive() {
        super.onActive();

    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    protected void onResult(int requestCode, Intent resultIntent) {
        super.onResult(requestCode, resultIntent);
        switch (requestCode){
            case 100:
                userName.setText(resultIntent.getStringParam("username"));
        }
    }

    @Override
    public void onClick(Component component) {
        if (component.getId() == ResourceTable.Id_to_login){
            Intent i = new Intent();
            presentForResult(new SystemLoginSlice(),i,100);
        }
        if (component.getId() == ResourceTable.Id_setting){
            Intent intent = new Intent();
            present(new SettingSlice(),intent);
        }
        if (component.getId() == ResourceTable.Id_history_data){
            Intent intent = new Intent();
            present(new HistoryDataSlice(),intent);
        }
    }
}
